/// -----------------------------------------------------------------------------------------------------------
/// Module      :  SalesForceWCFAdapterConnectionFactory.cs
/// Description :  Defines the connection factory for the target system.
/// -----------------------------------------------------------------------------------------------------------

#region Using Directives
using System;
using System.Collections.Generic;
using System.Text;
using System.IdentityModel.Selectors;
using System.ServiceModel.Description;
using System.Windows.Forms;

using Microsoft.ServiceModel.Channels.Common;
#endregion

namespace WCFLOBTalk.SalesForceWCF
{
    public class SalesForceWCFAdapterConnectionFactory : IConnectionFactory
    {
        #region Private Fields

        // Stores the client credentials
        private ClientCredentials clientCredentials;
        // Stores the adapter class
        private SalesForceWCFAdapter adapter;

        private SalesForceWCFAdapterConnectionUri connectionProperties;


        #endregion Private Fields

        /// <summary>
        /// Initializes a new instance of the SalesForceWCFAdapterConnectionFactory class
        /// </summary>
        public SalesForceWCFAdapterConnectionFactory(ConnectionUri connectionUri
            , ClientCredentials clientCredentials
            , SalesForceWCFAdapter adapter, string username, string password)
        {
            // cast the ConnectionUri back to the appropriate type

            //MessageBox.Show("Creating the factory now.");
            // so connectionUri does not have the properties like we need them!
            SalesForceWCFAdapterConnectionUri sfUri = connectionUri as SalesForceWCFAdapterConnectionUri;

            //System.Windows.Forms.MessageBox.Show(String.Format(" in factory ctor url: {0}, username: {1}, password: {2}, securityToken: {3}, version: {4}", sfUri.Uri.ToString(), clientCredentials.UserName.UserName, clientCredentials.UserName.Password, sfUri.SecurityToken, sfUri.version));

            this.connectionProperties = sfUri;

            // Add password storing in config eventually
            // setup the credentials here
            //if (username != null && password != null)
            //{
            //    ClientCredentials c = new ClientCredentials();
            //    c.UserName.UserName = username;
            //    c.UserName.Password = password;
            //    this.clientCredentials = c;
            //}
            //else
            //{
                this.clientCredentials = clientCredentials;
            //}
            this.adapter = adapter;
        }

        #region Public Properties

        /// <summary>
        /// Gets the adapter
        /// </summary>
        public SalesForceWCFAdapter Adapter
        {
            get
            {
                return this.adapter;
            }
        }

        public ClientCredentials Credentials
        {
            get
            {
                return this.clientCredentials;
            }
            set
            {
                this.clientCredentials = value;
            }

        }

        public SalesForceWCFAdapterConnectionUri ConnectionProperties
        {
            get
            {
                return this.connectionProperties;
            }
            set
            {
                this.connectionProperties = value;
            }
        }

        #endregion Public Properties

        #region Public Methods

        /// <summary>
        /// Creates the connection to the target system
        /// </summary>
        public IConnection CreateConnection()
        {
            return new SalesForceWCFAdapterConnection(this);
        }

        #endregion Public Methods
    }
}
